﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.Net.Mail;

namespace chad.azure.diagnostics.report
{
    class EmailProcessor : IDisposable
    {
        private String _smtpServerAddress;
        private int _smtpServerPort;

        private SmtpClient _smtp;

        public EmailProcessor(String smtpServerAddress, int smtpServerPort = 25)
        {
            // Assign global variables
            _smtpServerAddress = smtpServerAddress;
            _smtpServerPort = smtpServerPort;

            _smtp = new SmtpClient(_smtpServerAddress, _smtpServerPort);


        }

        public void SendEmail(String fromAddress, String[] toAddresses, String subject, String body, String[] filesToAttach)
        {
            MailMessage msg = new MailMessage();

            msg.From = new MailAddress(fromAddress);
            foreach(String to in toAddresses) {
                msg.To.Add(to);
            }
            msg.Subject = subject;
            msg.Body = body;

            foreach (String file in filesToAttach)
            {
                msg.Attachments.Add(new Attachment(file));
            }

            _smtp.Send(msg);
        }


        public void Dispose()
        {
            try
            {
                _smtp.Dispose();
            }
            catch (Exception ex) { }
        }
    }
}
